package com.example.content_provider_server.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.example.content_provider_server.constant.UserInfoContent;
import com.example.content_provider_server.database.UserDBHelper;

public class UserContentProvider extends ContentProvider {
    private UserDBHelper dbHelper;
    // 通过uri参数来传递信息
    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    // 删除所有模式
    private static final int USERS = 1;
    // 根据id删除模式
    private static final int USER = 0;

    static {
        //com.example.content_provider_server.provider.UserContentProvider/user 匹配结果为：1 即 USERS
        URI_MATCHER.addURI(UserInfoContent.AUTHORITIES, "user", USERS);
        //com.example.content_provider_server.provider.UserContentProvider/user/123 匹配结果为：0 即 USER
        URI_MATCHER.addURI(UserInfoContent.AUTHORITIES, "user/#", USER);
    }

    @Override
    public boolean onCreate() {
        Log.d("zhoutao", "UserContentProvider onCreate: ");
        dbHelper = UserDBHelper.getInstance(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Log.d("zhoutao", "UserContentProvider query: ");

        if (URI_MATCHER.match(uri) != USERS) {
            return null;
        }

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(UserDBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, null);

        return cursor;
    }


    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.d("zhoutao", "UserContentProvider insert: ");

        if (URI_MATCHER.match(uri) != USERS) {
            return null;
        }

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        long id = db.insert(UserDBHelper.TABLE_NAME, null, values);

        // 作为发布者，发布变化事件
//        if(id > 0) {
//            // 创建携带id的uri => "content://AUTHORITIES/user/123"
//            Uri newUri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI, id);
//            // 发布广播
//            getContext().getContentResolver().notifyChange(newUri, null);
//        }

        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase dbWriter = dbHelper.getWritableDatabase();
        int count = 0;

        // 根据uri匹配来判断 是删除多个还是单个
        int mode = URI_MATCHER.match(uri);
        switch (mode) {
            // url 为：com.example.content_provider_server.provider.UserContentProvider/user，匹配结果为 1，即 USERS
            // 根据条件 “DELETE  WHREE xx = yy” 删除所有
            case USERS:
                count = dbWriter.delete(UserDBHelper.TABLE_NAME, selection, selectionArgs);
                break;
            case USER:
                // url 为：com.example.content_provider_server.provider.UserContentProvider/user/123，
                // 匹配结果为 0，即 USER
                // 根据条件 _id 删除
                String userId = uri.getLastPathSegment(); // 获取uri最后的参数急_id
                count = dbWriter.delete(UserDBHelper.TABLE_NAME, "_id = ? ", new String[]{userId});
                break;
            default:
                break;
        }

        dbWriter.close();
        return count;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}